package com.ts.api.http.user.phone;

import com.alibaba.fastjson2.JSONObject;
import com.gitee.apanlh.util.algorithm.digest.DigestType;
import com.gitee.apanlh.util.algorithm.digest.SHA;
import com.gitee.apanlh.util.base.ChooseEq;
import com.gitee.apanlh.util.base.CollUtils;
import com.gitee.apanlh.util.base.Eq;
import com.gitee.apanlh.util.base.StringUtils;
import com.gitee.apanlh.util.date.DateUtils;
import com.gitee.apanlh.util.encode.Base64Utils;
import com.gitee.apanlh.util.id.IdUtils;
import com.gitee.apanlh.util.log.LogLevel;
import com.gitee.apanlh.util.net.http.HttpClientBuilder;
import com.gitee.apanlh.util.net.http.HttpResponse;
import com.gitee.apanlh.web.http.HttpMethod;
import com.ts.api.module.common.entity.bo.CheckPhoneStatusBO;

/**
 *  玄武云手机号检测状态
 *
 *  @author Pan
 */
//@Component
public class XwyPhoneStatusProvider implements UserPhoneAuthProvider {

    public static final String CHECK_PHONE_URL = "https://www.139130.com/ytx-api/v1.0.0/phone-number-detection/real-time/detection?sig={}";
    public static final String CHECK_PHONE_SID = "91a039e61d77f97544d3d1afde0553b0";
    public static final String CHECK_PHONE_TOKEN = "2a26450c48c23617f8590d5c91a779ca";

    @Override
    public CheckPhoneStatusBO checkPhoneStatus(String phone) {
        String time = DateUtils.currentTime("yyyyMMddHHmmss");
        String sign = SHA.create(DigestType.SHA_1).digestToHex(CHECK_PHONE_SID + CHECK_PHONE_TOKEN + time);

        HttpResponse response = HttpClientBuilder.builder(StringUtils.format(CHECK_PHONE_URL, sign), HttpMethod.POST)
                .withHeader("Accept", "application/json;charset=utf-8")
                .withHeader("Authorization", Base64Utils.encodeToStr(CHECK_PHONE_SID + ":" + time))
                .withBodyJson(newMap -> {
                    newMap.put("uuid", IdUtils.getSimpleUuid());
                    newMap.put("phones", CollUtils.newArrayList(newList -> {
                        newList.add(phone);
                    }));
                })
                .withLogLevel(LogLevel.WARN)
                .withErrorLogLevel(LogLevel.WARN)
                .build();

        String str = response.getStr();
        JSONObject jsonObject = JSONObject.parseObject(str);
        Integer code = jsonObject.getInteger("code");
        //  成功状态
        if (Eq.object(0, code)) {
            CheckPhoneStatusBO checkPhoneStatusBO = jsonObject.getJSONArray("phones").getJSONObject(0).toJavaObject(CheckPhoneStatusBO.class);
            //  根据三方接口定义来修改自己数据库中的状态-变更为用户信息表运营商状态及手机号码状态（玄武云）
            //  1	空号
            //  2	实号
            //  3	停机
            //  4	库无
            //  5	沉默号
            //  6	风险号
            //  7	错误号
            ChooseEq.create(checkPhoneStatusBO.getPhoneStatus())
                    .when("1", () -> checkPhoneStatusBO.setPhoneStatus("1"))
                    .when("2", () -> checkPhoneStatusBO.setPhoneStatus("0"))
                    .when("3", () -> checkPhoneStatusBO.setPhoneStatus("2"))
                    .when("4", () -> checkPhoneStatusBO.setPhoneStatus("1"))
                    .when("5", () -> checkPhoneStatusBO.setPhoneStatus("2"))
                    .when("6", () -> checkPhoneStatusBO.setPhoneStatus("2"))
                    .when("7", () -> checkPhoneStatusBO.setPhoneStatus("3"))
                    .end();

            //  1	移动
            //  2	联通
            //  3	电信
            ChooseEq.create(checkPhoneStatusBO.getCarrier())
                    .when("1", () -> checkPhoneStatusBO.setCarrier("1"))
                    .when("2", () -> checkPhoneStatusBO.setCarrier("2"))
                    .when("3", () -> checkPhoneStatusBO.setCarrier("3"))
                    .end();
            return checkPhoneStatusBO;
        }

        //  返回检测异常
        CheckPhoneStatusBO checkPhoneStatusBO = new CheckPhoneStatusBO();
        checkPhoneStatusBO.setPhoneStatus("100");
        checkPhoneStatusBO.setCarrier("3");
        return checkPhoneStatusBO;
    }
}
